package com.github.davidfantasy.mybatisplus.generatorui.util;

import lombok.extern.slf4j.Slf4j;

import java.net.Inet4Address;
import java.net.UnknownHostException;

@Slf4j
public class SerialVersionUID {

    private static long workerId;
    private static SnowflakeIdGenerator snowflakeIdGenerator;

    static {
        workerId = 1L;
        try {
            String hostName = Inet4Address.getLocalHost().getHostName();
            workerId = (long)((hostName.hashCode() & Integer.MAX_VALUE) % 32);
        } catch (UnknownHostException var4) {
            log.error("出初始化Snowflake获得当前主机名异常", var4);
        }
        snowflakeIdGenerator = new SnowflakeIdGenerator(workerId, 0L);
    }

    public static long generate() {
        return snowflakeIdGenerator.nextId();
    }

}
